package com.lottery.ruok.lottrey.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.adapter.ViewPagerTitleAdapter;
import com.lottery.ruok.lottrey.fragment.BaseFragment;
import com.lottery.ruok.lottrey.fragment.GetRedPacketFragment;
import com.lottery.ruok.lottrey.fragment.GivePrizeFragment;
import com.lottery.ruok.lottrey.fragment.SendRedPacketFragment;
import com.lottery.ruok.lottrey.ui.ShapeIndicatorView;
import com.lottery.ruok.lottrey.utils.DisplayUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/*
* 我的红包
* */

public class MyRedPacketActivity extends BaseActivity {

    @BindView(R.id.custom_indicator)
    ShapeIndicatorView customIndicator;
    @BindView(R.id.tabs)
    TabLayout tabs;
    @BindView(R.id.vp_view)
    ViewPager vpView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_red_packet_activity);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        ArrayList<BaseFragment> mViewList = new ArrayList<>();
        mViewList.add(new GetRedPacketFragment());
        mViewList.add(new SendRedPacketFragment());
        mViewList.add(new GivePrizeFragment());
        String[] s = {"收到红包", "发出红包", "打赏"};
        ViewPagerTitleAdapter mAdapter = new ViewPagerTitleAdapter(getSupportFragmentManager(), mViewList, s);
        vpView.setAdapter(mAdapter);
        if (mViewList.size() > 0) {
            vpView.setOffscreenPageLimit(mViewList.size());
        }
        tabs.setupWithViewPager(vpView);

        customIndicator.setupWithTabLayout(tabs);
        customIndicator.setupWithViewPager(vpView);

        if(getIntent().getBooleanExtra("isThreeTab", false)){
            vpView.post(new Runnable() {
                @Override
                public void run() {
                    vpView.setCurrentItem(2);
                }
            });
        }

        for (int i = 0; i < tabs.getTabCount(); i++) {
            TabLayout.Tab tab = tabs.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(getTabView(s[i], i, 0));
            }
        }
        vpView.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < tabs.getTabCount(); i++) {
                    if (i == position) {
                        tabs.getTabAt(i).getCustomView().findViewById(R.id.imageView).setVisibility(View.VISIBLE);
                    } else {
                        tabs.getTabAt(i).getCustomView().findViewById(R.id.imageView).setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    private View getTabView(String s, int position, int index){
        View view = LayoutInflater.from(this).inflate(R.layout.tab_item, null);
        TextView tv= view.findViewById(R.id.textView);
        ImageView line = view.findViewById(R.id.imageView);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.width = (int) DisplayUtil.sp2px(20*s.length(),this);
        lp.height = (int) DisplayUtil.dip2px(3,this);
        lp.gravity = Gravity.CENTER_HORIZONTAL;
        line.setLayoutParams(lp);
        tv.setText(s);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        if(position == index){
            view.findViewById(R.id.imageView).setVisibility(View.VISIBLE);
        }
        return view;
    }
}
